package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        // 调用mybatisplus的api进行列表查询
        // 1.参数校验
        if (null == channelDto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.分页参数的校验
        channelDto.checkParam();

        Page<AdChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());

        LambdaQueryWrapper<AdChannel> adChannelLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(channelDto.getName())) {
            adChannelLambdaQueryWrapper.like(AdChannel::getName, channelDto.getName());
        }
        IPage<AdChannel> adChannelIPage = page(page, adChannelLambdaQueryWrapper);

        PageResponseResult pageResponseResult =
                new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) adChannelIPage.getTotal());
        pageResponseResult.setData(adChannelIPage.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        // 1.参数校验
        if (null == adChannel) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.参数填充
        adChannel.setCreatedTime(new Date());
        // 3.调用实现数据库的数据插入
        boolean saveResult = this.save(adChannel);
        if (!saveResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FAIL);
        }
        // 4.返回前台
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {
        // 1.参数校验
        if (null == adChannel || null == adChannel.getId()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.填充参数 无
        // 3.调用接口实现修改
        boolean updateResult = this.updateById(adChannel);
        if (!updateResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL);
        }
        // 4.结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteAdChannel(Integer id) {
        if (null == id) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = this.getById(id);
        if (null == adChannel) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道有效不能删除");
        }
        boolean delResult = this.removeById(id);
        if (!delResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAIL);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
